<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Listing/classes/PhpFox_ComponentViewAll.class.php, $Author:  $ */



/** Display list classifieds
 *
 * Parameters:
 *  1. oUser (mandatory) - user account object
 *
 * @package module.listing
 * @version $Id:
 *   PHPFox Team ()
 */
class PhpFox_Mod_Listing_ComponentViewAll extends BaseComponent
{
	function process()
	{
	    //check permissions
        $oSecurityService = App::getModuleService('Account', 'Security');
        $oSecurityService->checkPerm('listing', array('read_only', 'full'));
		/* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
		$oListingService = $this->_oModule->getService('Listing');
		$oUser = $oSecurityService->getCurrentUser();
		$oReq = &App::getRequest();
		/* @var $oGroupsService PhpFox_Mod_Groups_ServiceGroups */
		App::loadClass('Filter');

		$aCond = array();
        $aCatHash = array();
        $aCatHash[''] = App::format('view_all');
        foreach($oListingService->getCategoryHash() as $i=>$sCat)
            $aCatHash[$i] = $sCat;

        $aCountry[''] = App::format('core.any');
        foreach(App::getTxtFile('country.txt') as $i=>$sCountry)
	        $aCountry[$sCountry] = $sCountry;
        $aState[''] = App::format('core.any');
        foreach(App::getTxtFile('us_stat.txt') as $i=>$sState)
	        $aState[$sState] = $sState;

        $aFilter = array
        (
            'location' => array
            (
                'value'  => '=',
                'select' => $aCountry,
            ),
            'state' => array
            (
                'value'  => '=',
                'select' => $aState,
            ),
            'list_id' => array
            (
                'value'  => '=',
                'select' => $aCatHash,
            ),
            'city'   => 'like',
            'title'  => 'like',
        );

        if ($oReq->getInt('cat'))
        {
            $aFilter['list_id']['def'] = $oReq->getInt('cat');
        }
        if($oReq->get('country'))
        {
            $aFilter['location']['def'] = $oReq->get('country');
            $aCond['location'] = '= "'.$oReq->get('country').'"';
        }
        $iPageSize = App::getParam('public.pagesize');
        $iPage = $oReq->getInt('page', 1);


        $sOrder = 'time DESC';

        $oFilter = &new Filter('listing', $aFilter);
        if($oReq->getInt('cat'))
        {
            $oFilter->reset();
            $oFilter->aData['list_id'] = $oReq->getInt('cat');
            $aCond['list_id'] = ' = '.$oReq->getInt('cat');
        }
        $oFilter->activateFilter();


        foreach ($oFilter->aConditions as $sKey => $sValue)
        {
             if($sKey == 'title')
             {
                $aCond[] = str_replace('{#title}', '{#text}', $sValue).' OR '.$sValue;
             }
             else
             {
                $aCond[] = $sValue;
             }
        }

        list($aListing, $iCnt) = $oListingService->getListingByCategory($aCond, $iPage, $iPageSize, $sOrder);

        foreach ($aListing as $i=>$aList)
        {
            $aListing[$i]['bloc'] = true;
            if(!is_readable(App::getParam('flag.pic.path').$aListing[$i]['location'].'.gif'))
            {
                $aListing[$i]['srcf'] = App::getParam('flag.pic.url').'Unknown.gif';
                $aListing[$i]['bloc'] = false;
            }
            else
                $aListing[$i]['srcf'] = App::getParam('flag.pic.url').$aListing[$i]['location'].'.gif';
        }
        $oPager = App::newObject('Pager', $iCnt, $iPage, $iPageSize);

        $oTpl = $this->_oModule->getTemplate();



        $oTpl->assignSrc(array(
                               'aFilter'      => $oFilter->aHtml,
                               'aErrors'      => $this->getErrors(),
                               'aListing'     => !empty($aListing) ? $aListing : '',
                               'aPaging'      => $oPager->getInfo(App::getUrl()),
                               'bZipState'   => App::getSetting('us_states'),
                         ));

        $srcHtml = $oTpl->fetch('ViewAll.html');
		return $srcHtml;
	}
}
?>